<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $partyItemDetailArray = array();
  $branchArray          = array();
  $partyArray           = array();
  $companyName          = "";
  $contactPerson1       = "";
  $contactPerson2       = "";
  $contactPerson3       = "";
  $address              = "";
  $mobile1              = "";
  $mobile2              = "";
  $mobile3              = "";
  $city                 = "";
  $email                = "";
  $web                  = "";
  $status               = "";
  $partyTINNo           = "";
  $partyId              = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
  $challanShort         = isset($_REQUEST['challanShort']) ? $_REQUEST['challanShort'] : "";
  $redirectFrom         = isset($_REQUEST['redirectFrom']) ? $_REQUEST['redirectFrom'] : "";
  $lastInserId          = 0;
  
  if(isset($_POST['cancelBtn']))
	{
    header("Location:party.php");
    exit();
	}
  // after post : start 
  if(isset($_POST['companyName']))
  {
    if($partyId > 0)
    {
      $updateParty = "UPDATE party SET companyName     = '".$_POST['companyName']."',
                                       contactPerson1  = '".$_POST['contactPerson1']."',
                                       contactPerson2  = '".$_POST['contactPerson2']."',
                                       contactPerson3  = '".$_POST['contactPerson3']."',
                                       phoneNo         = '".$_POST['phoneNo']."',
                                       branchId        = ".$_POST['branchId'].",
                                       address         = '".$_POST['address']."',
                                       city            = '".$_POST['city']."',
                                       mobile1         = '".$_POST['mobile1']."',
                                       mobile2         = '".$_POST['mobile2']."',
                                       mobile3         = '".$_POST['mobile3']."',
                                       email           = '".$_POST['email']."'
                                 WHERE partyId         = ".$_REQUEST['partyId']." ";
      $updatePartyRes   = mysql_Query($updateParty);
      if(!$updatePartyRes)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:party.php");
    }
    else
    {
      $address = nl2br(addslashes($_POST['address']));
      
      $insertParty = "INSERT INTO party (companyName,contactPerson1,contactPerson2,contactPerson3,phoneNo,branchId,address,city,mobile1,mobile2,mobile3,email)
                            VALUE('".nl2br(addslashes($_POST['companyName']))."','".nl2br(addslashes($_POST['contactPerson1']))."','".nl2br(addslashes($_POST['contactPerson2']))."','".nl2br(addslashes($_POST['contactPerson3']))."',
                                  '".$_POST['phoneNo']."',".$_POST['branchId'].",'".$address."','".$_POST['city']."','".$_POST['mobile1']."',
                                  '".$_POST['mobile2']."','".$_POST['mobile3']."','".$_POST['email']."')";
      $insertPartyResult = mysql_query($insertParty);
      $lastInserId =  mysql_insert_id();
      if(!$insertPartyResult)
        die("Insert Query Not Inserted : ".mysql_error());
      elseif($redirectFrom == "dailyReport")
      {
        header("Location:dailyReport.php");
      }
      elseif($redirectFrom == "complainSevice")
      {
        header("Location:complainService.php");
      }
      elseif($redirectFrom == "newInstallation")
      {
        header("Location:newInstallation.php");
      }
      elseif($redirectFrom == "demo")
      {
        header("Location:demo.php");
      }
      elseif($redirectFrom == "rAndD")
      {
        header("Location:rAndD.php");
      }
      elseif($redirectFrom == "others")
      {
        header("Location:others.php");
      }
      elseif($redirectFrom == "deliveryChalan")
      {
        header("Location:deliveryChalan.php?lastInserId=".$lastInserId."&challanShort=".$challanShort);
      }
      else
      {
        header("Location:party.php");
      }
    }
  }    
  // after post : end
  
  $branch = "SELECT branchId,branchName
               FROM branch";
  $branchResult = mysql_query($branch);
  $a = 0;  
  while ($branchRow = mysql_fetch_array($branchResult))
  {
  	 if($a == 0 || (isset($_REQUEST['branchId']) && $_REQUEST['branchId'] == $branchRow['branchId']))
	    $branchSelected = $branchRow['branchId']; 
	    
  	$branchArray['branchId'][$a]   = $branchRow['branchId'];
  	$branchArray['branchName'][$a] = $branchRow['branchName'];
  	$a++;
  }
  
  $party = "SELECT partyId,companyName
              FROM party
             WHERE status = 'A'
          ORDER BY companyName ASC";
  $partyResult = mysql_query($party);
  $b = 0;  
  while ($partyRow = mysql_fetch_array($partyResult))
  {
  	 if($b == 0 || (isset($_REQUEST['partyId']) && $_REQUEST['partyId'] == $partyRow['partyId']))
	    $partySelected = $partyRow['partyId']; 
	    
  	$partyArray['partyId'][$b]   = $partyRow['partyId'];
  	$partyArray['companyName'][$b] = substr_replace($partyRow['companyName'],'...',40);
  	$b++;
  }
  
   $selectParty = "SELECT party.partyId,party.companyName,party.contactPerson1,party.contactPerson2,party.contactPerson3,party.phoneNo,
                         party.branchId,party.address,party.city,party.mobile1,party.mobile2,party.mobile3,party.email
                    FROM party
                    WHERE status = 'A'
                ORDER BY party.companyName";
  $selectpartyResult = mysql_query($selectParty);
  $i = 0;
  while($partyRow = mysql_fetch_array($selectpartyResult))
  {
    $partyItemDetailArray[$i]['partyId']        = $partyRow['partyId'];
    $partyItemDetailArray[$i]['companyName']    = $partyRow['companyName'];
    $partyItemDetailArray[$i]['contactPerson1'] = $partyRow['contactPerson1'];
    $partyItemDetailArray[$i]['contactPerson2'] = $partyRow['contactPerson2'];
    $partyItemDetailArray[$i]['contactPerson3'] = $partyRow['contactPerson3'];
    $partyItemDetailArray[$i]['phoneNo']        = $partyRow['phoneNo'];
    $partyItemDetailArray[$i]['address']    		= $partyRow['address'];
    $partyItemDetailArray[$i]['city']       		= $partyRow['city'];
    $partyItemDetailArray[$i]['mobile1']    		= wordwrap($partyRow['mobile1'],12,"<br />\n",TRUE);
    $partyItemDetailArray[$i]['mobile2']    		= $partyRow['mobile2'];
    $partyItemDetailArray[$i]['mobile3']    		= $partyRow['mobile3'];
    $partyItemDetailArray[$i]['email']      		= $partyRow['email'];
    
    if($partyRow['partyId'] == $partyId)
    {
      $companyName        = $partyRow['companyName'];
      $contactPerson1     = $partyRow['contactPerson1'];
      $contactPerson2     = $partyRow['contactPerson2'];
      $contactPerson3     = $partyRow['contactPerson3'];
      $phoneNo     			  = $partyRow['phoneNo'];
      $address    			  = $partyRow['address'];
      $city       			  = $partyRow['city'];
      $mobile1    			  = $partyRow['mobile1'];
      $mobile2    			  = $partyRow['mobile2'];
      $mobile3    			  = $partyRow['mobile3'];
      $email      			  = $partyRow['email'];
    }
    $i++;
  }
   
  include("./bottom.php");
  $smarty->assign("challanShort",$challanShort);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("companyName",$companyName);
  $smarty->assign("contactPerson1",$contactPerson1);
  $smarty->assign("contactPerson2",$contactPerson2);
  $smarty->assign("contactPerson3",$contactPerson3);
  $smarty->assign("address",$address);
  $smarty->assign("mobile1",$mobile1);
  $smarty->assign("mobile2",$mobile2);
  $smarty->assign("mobile3",$mobile3);
  $smarty->assign("city",$city);
  $smarty->assign("email",$email);
  $smarty->assign("web",$web);
  $smarty->assign("status",$status);
  $smarty->assign("branchSelected",$branchSelected);
  $smarty->assign("partyItemDetailArray",$partyItemDetailArray);
  $smarty->assign("branchArray",$branchArray);
  $smarty->assign("partyArray",$partyArray);
  $smarty->assign("redirectFrom",$redirectFrom);
  $smarty->assign("lastInserId",$lastInserId);
  $smarty->display("party.tpl");
}
?>